package com.hiyee.customer.service.impl;

import com.hiyee.customer.mapper.CustomerAccountMapper;
import com.hiyee.customer.mapper.CustomerMapper;
import com.hiyee.customer.repository.CustomerRepo;
import com.hiyee.customer.service.CustomerService;
import com.hiyee.enums.*;
import com.hiyee.exception.GraceException;
import com.hiyee.idWorker.SnowflakeIdUtil;
import com.hiyee.pojo.Customer;
import com.hiyee.pojo.CustomerAccount;
import com.hiyee.pojo.auth.CustomerTokenInfo;
import com.hiyee.pojo.repo.CustomerInfoRepo;
import com.hiyee.pojo.vo.CustomerInfoVO;
import com.hiyee.response.ResponseStatusEnum;
import com.hiyee.utils.BnBuilderUtil;
import com.hiyee.utils.DateTimeUtil;
import com.hiyee.utils.JWTUtil;
import com.hiyee.utils.ToolUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;

/**
 * 会员业务实现类
 *
 * @author WangBin
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerRepo customerRepo;
    @Autowired
    private SnowflakeIdUtil snowflakeIdUtil;
    @Autowired
    private BnBuilderUtil bnBuilderUtil;
    @Autowired
    private CustomerAccountMapper customerAccountMapper;

    @Override
    public Customer getByUsername(String username) {
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        return customerMapper.selectOneByExample(example);
    }

    @Override
    public Customer getByMobile(String mobile) {
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile", mobile);
        return customerMapper.selectOneByExample(example);
    }

    @Override
    public Customer getById(Long customerId) {
        Example example = new Example(Customer.class);
        return customerMapper.selectByPrimaryKey(customerId);
    }

    @Override
    public Customer getByToken(String token) {
        // 解析 token
        CustomerTokenInfo customerTokenInfo = JWTUtil.verify(token);
        System.out.println("客户id：" + customerTokenInfo.getCustomerId());
        if(customerTokenInfo.getCustomerId() == null)return null;
        return this.getById(customerTokenInfo.getCustomerId());
    }

    @Override
    public CustomerInfoRepo getInfo(Long id) {
        CustomerInfoRepo customer = customerRepo.getById(id);
        if(customer == null) return null;
        return customer;
    }

    @Override
    @Transactional
    public Customer registerByMobile(String mobile) {
        // 根据手机号查询客户
        Customer findCustomer = customerRepo.getByMobile(mobile);
        if(findCustomer != null) {
            GraceException.display(ResponseStatusEnum.CUSTOMER_MOBILE_EXIST);
        }
        // 组建客户信息
        Customer customer = new Customer();
        customer.setCustomerId(snowflakeIdUtil.nextId());
        customer.setCustomerBn(bnBuilderUtil.build(BnFormatEnum.CUSTOMER_BN));
        customer.setNickname(ToolUtil.maskMobile(mobile));
        customer.setRealname(customer.getNickname());
        customer.setMobile(mobile);
        customer.setUsername(mobile);
        customer.setPassword(ToolUtil.hash(mobile));
        customer.setGender(GenderEnum.MALE.getValue());
        customer.setStoreId(1L);
        customer.setGroupId(4);
        customer.setGradeId(1);
        customer.setBalance(new BigDecimal(0));
        customer.setIsDeleted(0);
        customer.setStatus(AuditStatusEnum.WAIT_AUDIT.getValue());
        customer.setCustomerType(CustomerTypeEnum.NORMAL.getValue());
        customer.setIsActive(ActiveStatusEnum.ACTIVE.getValue());
        customer.setRegisterType(RegisterTypeEnum.AUTO.getValue());
        customer.setCreatedAt(DateTimeUtil.getDateTimeStr("yyyyMMdd HH:mm:ss"));
        customer.setUpdatedAt(DateTimeUtil.getDateTimeStr("yyyyMMdd HH:mm:ss"));

        Example example = new Example(Customer.class);
        customerMapper.insert(customer);

        // 创建账户信息
        Example accountExample = new Example(CustomerAccount.class);
        CustomerAccount customerAccount = new CustomerAccount();
        customerAccount.setAccountId(snowflakeIdUtil.nextId());
        customerAccount.setCustomerName(customer.getNickname());
        customerAccount.setStoreId(1L);
        customerAccount.setCustomerId(customer.getCustomerId());
        customerAccount.setBalance(new BigDecimal(0));
        customerAccount.setStatus(ActiveStatusEnum.ACTIVE.getValue());
        customerAccount.setCreatedAt(DateTimeUtil.getDateTimeStr("yyyyMMdd HH:mm:ss"));
        customerAccount.setUpdatedAt(DateTimeUtil.getDateTimeStr("yyyyMMdd HH:mm:ss"));
        customerAccountMapper.insert(customerAccount);

        return customer;
    }
}
